// 页面加载进度条
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

let count = 0 // 显示loading请求数量
NProgress.configure({ showSpinner: false }) // NProgress Configuration

/**
 * 显示loading
 */
function showLoading () {
  count++
  NProgress.start()
  // console.log('显示loading：' + count)
}

/**
 * 隐藏loading
 */
function hideLoading () {
  setTimeout(() => {
    count--
    if (count <= 0) {
      count = 0
      NProgress.done()
    }
    // console.log('隐藏loading：' + count)
  }, 200)
}

/**
 * 初始化路由loading
 */
function initRouterLoading (router) {
  // 路由切换开始
  router.beforeEach((from, to, next) => {
    showLoading()
    next()
  })

  // 路由切换结束
  router.afterEach((from, to) => {
    hideLoading()
  })
}

export {
  showLoading,
  hideLoading,
  initRouterLoading
}
